var flagDate = undefined;
var balanceInitSettingData = undefined;
var chargingFixedPriceNameList = [];
var summaryBalanceData = [];
var subBranchCompanyBalanceData = [];
var lineBalanceData = [];
var totalChargingKWHData = [];
var chargingCountData = [];
var totalPriceData = [];
var subBranchCompanyNameData = [];
var balanceBillNameList = [];
var lineNameList = [];
var tabFlag = "summaryBalance";
var exportFlag = undefined;
var balanceDay = undefined;
var lineInfoData = [];
var lineNameData = [];

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	$ajaxList('searchBalanceInitSetting', getBalanceInitSetting);
	$ajaxList('branchCompany', getBranchCompany);
	$("#BalanceRatio").hide();
	setTimeout(hideLoading, 500);
})
function getBalanceInitSetting(data){
	balanceInitSettingData = data.balanceInitSettingList[0]
	var balanceStartMonth = balanceInitSettingData && balanceInitSettingData.balanceStartMonth || new Date().getFullYear() + (new Date().getMonth() + 1 < 10 ? "0" + (new Date().getMonth() + 1) : new Date().getMonth() + 1);
	balanceDay = balanceInitSettingData.balanceDay;
	$("#balanceRatio").val(balanceInitSettingData.defaultFixedRatio);
	changeYear(0, balanceStartMonth);
	showMonth(getSystemTime().getMonth() + 1);
}
function initChargingPriceName(type) {
	var selectLength = $("#EffectiveDate option").length;
	if (selectLength < 1) {
		getChargingPriceName(type);
	}
}
function getChargingPriceName(type) {
	if (chargingFixedPriceNameList.length > 1) {
		getChargingFixedPriceName(chargingFixedPriceNameList);
	} else {
		$ajaxList('chargingFixedPriceName', getChargingFixedPriceName);
	}
}
function getChargingFixedPriceName(data) {
	chargingFixedPriceNameList = data;
	$("#ChargingPrice").empty();
	$("#ChargingUnitPrice").empty();
	$("#EffectiveDate").empty();
	var length = data.length;
	if (length == 0) {
		initTable();
		$("#EffectiveDate").attr('disabled', 'disabled');
		showMessge(messageContent["Balance.ChargingPriceNotFound"]);
		return;
	}
	var chargingPrice = undefined;
	for (var i = 0; i < length; i++) {
		chargingPrice = data[i].chargingPrice;
		if (data[i].enabled) {
			$("#EffectiveDate").append("<option value='" + chargingPrice + "' selected='selected'>" + date2String(data[i].effectiveDTTM) + "</option>");
			getChargingPrice(chargingPrice);
			$("#ChargingUnitPrice").val(data[i].chargingPrice);
		} else {
			$("#EffectiveDate").append("<option value='" + chargingPrice + "'>" + date2String(data[i].effectiveDTTM) + "</option>");
		}
		$("#EffectiveDate").attr('disabled', false);
	}
	if (!$("#ChargingPrice").text()) {
		$("#ChargingUnitPrice").val(data[i].chargingPrice);
		getChargingPrice(data[i].chargingPrice);
	}
}
function getChargingPrice(chargingPrice) {
	$("#ChargingPrice").text('固：' + chargingPrice + '元');
	$("#ChargingUnitPrice").val(chargingPrice);
	if(chargingPrice == 0) {
		$("#balanceRatio").val(0);
	} else {
		$("#balanceRatio").val(balanceInitSettingData.defaultFixedRatio);
	}
}
//function getBalanceBillName(data) {
//	balanceBillNameList = data || [];
//	$("#BalanceBillID").empty();
//	$("#BalanceBillID").append("<option value='' selected='selected'>请选择</option>");
//	$("#billErrorMessage").hide();
//	$("#billErrorMessage").empty();
//	var length = data.length;
//	if (length == 0) {
//		$("#billErrorMessage").show();
//		$("#billErrorMessage").text(messageContent["Balance.MonthBalanceBillNotExist"]);
//		return;
//	} else {
//		for (var i = 0; i < length; i++) {
//			$("#BalanceBillID").append("<option value='" + data[i].balanceBillID + "'>" + data[i].balanceBillName + "</option>");
//		}
//		if (!$("#BalanceBillID").val()) {
//			$("#BalanceBillID").tips({
//				side : 2,
//				msg : "请选择电力结算单",
//				bg : '#AE81FF',
//				time : 1
//			});
//			$("#BalanceBillID").focus();
//			initTable();
//			return;
//		}
//	}
//}
//function getBalanceBill(index) {
//	$("#CostRatioName").empty();
//	if (!$("#BalanceBillID").val()) {
//		$("#BalanceBillID").tips({
//			side : 2,
//			msg : "请选择电力结算单",
//			bg : '#AE81FF',
//			time : 1
//		});
//		$("#BalanceBillID").focus();
//		initTable();
//		return;
//	}
//	allClick(tabFlag);
//}
function initTable() {
	$("#summaryBalanceTbody").empty();
	$("#subBranchCompanyBalanceTbody").empty();
	$("#lineBalanceTbody").empty();
	$("#totalChargingCount").html(0);
	$("#totalTradeKWH").html(0);
	$("#totalChargingKWH").html(0);
	$("#totalChargingCost").html("0.00");
	$("#sbcChargingKWH").html(0);
	$("#sbcElectricCost").html("0.00");
	$("#sbcUServicePrice").html("0.00");
	$("#sbcChargingCost").html("0.00");
	$("#lineChargingCount").html(0);
	$("#lineCarCount").html(0);
	$("#lineChargingKWH").html(0);
	$("#lineChargingCost").html("0.00");
}
function count(data) {
    var res = [], hash = {};
    for(var i=0, elem; (elem = data[i]) != null; i++)  {
        if (!hash[elem])
        {
            res.push(elem);
            hash[elem] = true;
        }
    }
    return res;
}
function changeBranchCompany() {
	allClick(tabFlag);
}
function allClick(flag){
    $(".costMenu").removeClass("active");
	tabFlag = flag;
	var table = undefined;
	if(tabFlag == "summaryBalance"){
		$("#summary").addClass("active");
		$(".tab-pane").removeClass("active");
		$("#summaryBalance").addClass("active");
		$("#branchOffice").hide();
		$("#XLbranchOffice").hide();
		$("#phaseOffice").hide();
		$("#partWhiteTH").show();
		$("#subBranchCompanyName").val("");
		$("#names").val("");
		table = document.getElementById('summaryBalanceThead');
		rows = table.rows && table.rows.length;
		if (rows == 0) {
			createTableHead("summaryBalanceThead", summaryBalanceTable);
		}
	} else if (tabFlag == "subBranchCompanyBalance") {
		$("#presentation").addClass("active");
		$(".tab-pane").removeClass("active");
		$("#subBranchCompanyBalance").addClass("active");
		$("#branchOffice").show();
		$("#tabContent").show();
		$("#XLbranchOffice").hide();
		$("#phaseOffice").hide();
		$("#partWhiteTH").show();
		$("#subBranchCompanyName").val("");
		$("#names").val("");
		table = document.getElementById('subBranchCompanyBalanceThead');
		rows = table.rows && table.rows.length;
		if (rows == 0) {
			createTableHead("subBranchCompanyBalanceThead", subBranchCompanyBalanceTable);
		}
		$ajaxList('subBranchCompany', getSubBranchCompanyName, {branchCompanyID : $("#branchCompanyID").val()});
	} else if (tabFlag == "lineBalance") {
		$("#line").addClass("active");
		$(".tab-pane").removeClass("active");
		$("#lineBalance").addClass("active");
		$("#branchOffice").hide();
		$("#XLbranchOffice").show();
		$("#phaseOffice").hide();
		$("#partTableXLTJ").show();
		$("#partWhiteTH").hide();
		$("#subBranchCompanyName").val("");
		$("#names").val("");
		table = document.getElementById('lineBalanceThead');
		rows = table.rows && table.rows.length;
		if (rows == 0) {
			createTableHead("lineBalanceThead", lineBalanceTable);
		}
		lineNameData = [];
		$ajaxList('lineName', getLineName, {branchCompanyID : $("#branchCompanyID").val()});
	} else if (tabFlag == "phaseSummary") {
		$("#partWhiteTH").hide();
		$("#phase").addClass("active");
		$(".tab-pane").removeClass("active");
		$("#phaseSummary").addClass("active");
		$("#branchOffice").hide();
		$("#XLbranchOffice").hide();
		$("#phaseOffice").show();
		$("#subBranchCompanyName").val("");
		$("#names").val("");
		table = document.getElementById('phaseSummaryThead');
		rows = table.rows && table.rows.length;
		if (rows == 0) {
			createTableHead("phaseSummaryThead", phaseSummaryTable);
		}
		selectDate("chargingDate");
		$ajaxList('subBranchCompany', getSubBranchCompanyPhase, {branchCompanyID : $("#branchCompanyID").val()});
		return;
	} else if (tabFlag == "stageSummary") {
		$("#partWhiteTH").hide();
		$("#stage").addClass("active");
		$(".tab-pane").removeClass("active");
		$("#stageSummary").addClass("active");
		$("#branchOffice").hide();
		$("#XLbranchOffice").hide();
		$("#phaseOffice").hide();
		$("#chargingStationStage").val("");
		$("#names").val("");
		table = document.getElementById('stageSummaryThead');
		rows = table.rows && table.rows.length;
		if (rows == 0) {
			createTableHead("stageSummaryThead", stageSummaryTable);
		}
	}
	search();
}
function allMouse(ID, flag){
}
function getBranchCompany(data) {
	var length = data.length;
	for (var i = 0;i < length; i++){
        $("#branchCompanyID").append("<option value='" + data[i].branchCompanyID + "'>" + data[i].branchCompanyName + "</option>");
    }
	$("#branchCompanyID").val("2");
}
function getSubBranchCompanyName(data) {
	$("#subBranchCompanyName").empty();
	$("#subBranchCompanyName").append("<option value=''>请选择</option>");
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#subBranchCompanyName").append("<option value='" + data[i].subBranchCompanyID + "'>" + data[i].subBranchCompanyName + "</option>");
	}
}
function getLineName(data) {
	lineNameData = [];
	var length = data.length;
	lineInfoData = data;
	var item = undefined;
	for (var i = 0; i < length; i++) {
		$("#lineID").append("<option value='" + data[i].lineID + "'>" + data[i].lineName + "</option>");
		item = {};
		item.ID = data[i].lineID;
		item.label = data[i].lineName;
		item.value = data[i].lineName;
		lineNameData.push(item);
	}
	$(function(){
		autoComplete("names", lineNameData, '', "lineID");
		bindInput("names", onLineInput);  
	});
}
function onLineInput(){
	$("#lineID").val('');
}
function getLineID(){
	var value = $("#names").val();
	if (!value) {
		$("#lineID").val('');
	} else {
		if (!$("#lineID").val() && lineNameData && lineNameData.length > 0) {
			var matcherData = filter(lineNameData, value);
			if (matcherData) {
				var length = matcherData.length;
				for (var i=0; i<length; i++) {
					if (matcherData[i] && matcherData[i].value == value) {
						$("#lineID").val(matcherData[i].ID);
						break;
					}
				}
			}
		}
	}
}
function getSubBranchCompanyPhase(data) {
	$("#subBranchCompanyPhase").empty();
	$("#subBranchCompanyPhase").append("<option value=''>请选择</option>");
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#subBranchCompanyPhase").append("<option value='" + data[i].subBranchCompanyID + "'>" + data[i].subBranchCompanyName + "</option>");
	}
}
function search(){
	initTable();
	$("#lineIDSubmit").val('');
	$("#subBranchCompanyIDSubmit").val('');
	if (!$("#branchCompanyID").val()) {
		$("#branchCompanyID").tips({
			side : 2,
			msg : "请选择运营公司",
			bg : '#AE81FF',
			time : 2
		});
		$("#branchCompanyID").focus();
		return;
	}
	if (!$("#EffectiveDate").val()) {
		$("#EffectiveDate").tips({
			side : 2,
			msg : "请选择电价有效日期",
			bg : '#AE81FF',
			time : 1
		});
		$("#EffectiveDate").focus();
		return;
	}/* else if (!$("#BalanceBillID").val()) {
		$("#BalanceBillID").tips({
			side : 2,
			msg : "请选择电力结算单",
			bg : '#AE81FF',
			time : 1
		});
		$("#BalanceBillID").focus();
		return;
	}*/ else {
		if (tabFlag == "summaryBalance") {
			$ajax('searchSummaryBalance', 'costBalanceSearch', searchSummaryBalance);
		} else if (tabFlag == "subBranchCompanyBalance") {
			if (!$("#subBranchCompanyName").val()) {
				$("#subBranchCompanyName").focus();
				$("#subBranchCompanyName").tips({
					side : 2,
					msg : "请选择分公司",
					bg : '#AE81FF',
					time : 1
				});
			} else {
				$("#subBranchCompanyIDSubmit").val($("#subBranchCompanyName").val());
				$ajax('searchSubBranchCompanyBalance', 'costBalanceSearch', searchSubBranchCompanyBalance);
			}
		} else if (tabFlag == "lineBalance") {
			if (!$("#names").val()) {
				$("#names").focus();
				$("#names").tips({
					side : 2,
					msg : "请选择线路",
					bg : '#AE81FF',
					time : 1
				});
				return;
			} else {
				$("#lineIDSubmit").val($("#lineID").val());
				$ajax('searchLineBalance', 'costBalanceSearch', searchLineBalance);
			}
		} else if (tabFlag == "stageSummary") {
			$ajax('searchStageSummary', 'costBalanceSearch', searchStageSummary);
		}
	}
}
function searchSummaryBalance(data){
	subBranchCompanyNameData = [];
	totalChargingKWHData = [];
	chargingCountData = [];
	totalPriceData = [];
	$("#totalChargingCount").html(data.totalChargingCount || 0);
	$("#totalTradeKWH").html((data.allTotalTradeKWH || 0).toFixed(0));
	$("#totalChargingKWH").html((data.allTotalChargingKWH || 0).toFixed(0));
	$("#totalChargingCost").html((data.totalChargingPrice || 0).toFixed(2));
	summaryBalanceData = data && data.summaryBalanceList || [];
	createTableBody("summaryBalanceTbody", summaryBalanceTable, summaryBalanceData, true, true);
	for(var i=0; i<summaryBalanceData.length; i++){
		subBranchCompanyNameData.push(summaryBalanceData[i].subBranchCompanyName);
		totalChargingKWHData.push(summaryBalanceData[i].totalChargingKWH);
		chargingCountData.push(summaryBalanceData[i].chargingCount);
		totalPriceData.push(summaryBalanceData[i].totalPrice.toFixed(2));
	}
	changeSumEchart();
	setTimeout(hideLoading, 500);
}
function searchSubBranchCompanyBalance(data){
	var stationNameList = [];
	lineNameList = [];
	totalPriceData = [];
	balanceMonth = $("#balanceMonth").val();
	var startDate = balanceMonth.substring(0, 4) + "-" + balanceMonth.substring(4, 6) + "-" + balanceDay;
	var array = startDate.split("-");
	if(array[2] == "01") {
		startDate = date2String(new Date(array[0], array[1] - 1, array[2]));
	} else {
		startDate = date2String(new Date(array[0], array[1] - 2, array[2]));
	}
	var endDate = balanceMonth.substring(0, 4) + "-" + balanceMonth.substring(4, 6) + "-" + (balanceDay - 1);
	var endArray = endDate.split("-");
	if(endArray[2] == "0"){
		endDate = date2String(new Date(endArray[0], endArray[1], endArray[2]));
	} else {
		endDate = date2String(new Date(endArray[0], endArray[1] - 1, endArray[2]));
	}
	subBranchCompanyBalanceData = data && data.subBranchCompanyBalanceList || [];
	var subBranchCompanyBalanceTbody = $("#subBranchCompanyBalanceTbody");
	subBranchCompanyBalanceTbody.empty();
	var length = subBranchCompanyBalanceData.length;
	var effectiveDTTM = undefined;
	var rowspanCount = undefined;
	var sumChargingKWH = 0;
	var sumBalanceKWH = 0;
	var sumKWHPrice = 0;
	var sumServicePrice = 0;
	var sumTotalPrice = 0;
	for(var i=0; i<length + 1; i++){
		if (i == length || effectiveDTTM != subBranchCompanyBalanceData[i].effectiveDTTM) {
			effectiveDTTM = subBranchCompanyBalanceData[i] && subBranchCompanyBalanceData[i].effectiveDTTM;
			if (rowspanCount) {
				var rowCount = 1;
				var oilPrice = 0;
				var subtotalChargingKWH = 0;
				var subtotalTradeKWH = 0;
				var subtotalChargingPrice = 0;
				var subtotalServicePrice = 0;
				var subtotalPrice = 0;
				for (var j=i - rowspanCount; j<i; j++) {
					oilPrice = subBranchCompanyBalanceData[j].oilPrice;
					subtotalChargingKWH = floatObj.add(subtotalChargingKWH, subBranchCompanyBalanceData[j].totalChargingKWH);
					subtotalTradeKWH = floatObj.add(subtotalTradeKWH, subBranchCompanyBalanceData[j].totalTradeKWH);
					subtotalChargingPrice = floatObj.add(subtotalChargingPrice, subBranchCompanyBalanceData[j].chargingPrice);
					subtotalServicePrice = floatObj.add(subtotalServicePrice, subBranchCompanyBalanceData[j].servicePrice);
					subtotalPrice = floatObj.add(subtotalPrice, subBranchCompanyBalanceData[j].totalPrice);
					$tr = $('<tr>').appendTo(subBranchCompanyBalanceTbody);
					$("<td>").html(rowCount).appendTo($tr);
					rowCount++;
					if (j==i - rowspanCount) {
						if(subBranchCompanyBalanceData[i]){
							var oilStopDate = subBranchCompanyBalanceData[i].effectiveDTTM;
							var array = oilStopDate.split("-");
							oilStopDate = date2String(new Date(array[0], array[1] - 1, array[2] -1));
							$('<td rowspan="'+ rowspanCount + '">').html(compareDate(startDate, subBranchCompanyBalanceData[i-1].effectiveDTTM, true) + "——" + compareDate(endDate, oilStopDate, false)).appendTo($tr);
						} else {
							$('<td rowspan="'+ rowspanCount + '">').html(compareDate(startDate, subBranchCompanyBalanceData[i-1].effectiveDTTM, true) + "——" + endDate).appendTo($tr);
						}
					}
					$("<td>").html(subBranchCompanyBalanceData[j].chargingStationName).appendTo($tr);
					$("<td>").html(subBranchCompanyBalanceData[j].teamName).appendTo($tr);
					$("<td>").html(subBranchCompanyBalanceData[j].lineName).appendTo($tr);
					$("<td>").html(subBranchCompanyBalanceData[j].oilPrice).appendTo($tr);
					$("<td>").html(subBranchCompanyBalanceData[j].totalChargingKWH).appendTo($tr);
					$("<td>").html(subBranchCompanyBalanceData[j].balanceRatio).appendTo($tr);
					$("<td>").html(subBranchCompanyBalanceData[j].totalTradeKWH.toFixed(4)).appendTo($tr);
					$("<td>").html(subBranchCompanyBalanceData[j].chargingUnitPrice.toFixed(4)).appendTo($tr);
					$("<td>").html(subBranchCompanyBalanceData[j].chargingPrice.toFixed(4)).appendTo($tr);
					$("<td>").html(subBranchCompanyBalanceData[j].serviceUnitPrice.toFixed(4)).appendTo($tr);
					$("<td>").html(subBranchCompanyBalanceData[j].servicePrice.toFixed(4)).appendTo($tr);
					$("<td>").html(subBranchCompanyBalanceData[j].totalPrice.toFixed(4)).appendTo($tr);
				}
				sumChargingKWH = floatObj.add(sumChargingKWH, subtotalChargingKWH);
				sumBalanceKWH = floatObj.add(sumBalanceKWH, subtotalTradeKWH);
				sumKWHPrice = floatObj.add(sumKWHPrice, subtotalChargingPrice);
				sumServicePrice = floatObj.add(sumServicePrice, subtotalServicePrice);
				sumTotalPrice = floatObj.add(sumTotalPrice, subtotalPrice);
				$tr = $('<tr>').appendTo(subBranchCompanyBalanceTbody);
				$("<td>").html("").appendTo($tr);
				$('<td colspan="4" style="font-weight: bold;">').html("小计").appendTo($tr);
				$("<td>").html(oilPrice).appendTo($tr);
				$("<td>").html(subtotalChargingKWH).appendTo($tr);
				$("<td>").html("——").appendTo($tr);
				$("<td>").html(subtotalTradeKWH.toFixed(4)).appendTo($tr);
				$("<td>").html("——").appendTo($tr);
				$("<td>").html(subtotalChargingPrice.toFixed(2)).appendTo($tr);
				$("<td>").html("——").appendTo($tr);
				$("<td>").html(subtotalServicePrice.toFixed(2)).appendTo($tr);
				$("<td>").html(subtotalPrice.toFixed(2)).appendTo($tr);
			}
			rowspanCount = 1;
		} else {
			rowspanCount++;
		}
	}
	$tr = $('<tr>').appendTo(subBranchCompanyBalanceTbody);
	$("<td>").html("").appendTo($tr);
	$('<td colspan="4" style="font-weight: bold;">').html("总计").appendTo($tr);
	$("<td>").html(oilPrice).appendTo($tr);
	$("<td>").html(sumChargingKWH).appendTo($tr);
	$("<td>").html("——").appendTo($tr);
	$("<td>").html(sumBalanceKWH.toFixed(4)).appendTo($tr);
	$("<td>").html("——").appendTo($tr);
	$("<td>").html(sumKWHPrice.toFixed(2)).appendTo($tr);
	$("<td>").html("——").appendTo($tr);
	$("<td>").html(sumServicePrice.toFixed(2)).appendTo($tr);
	$("<td>").html((parseFloat(sumKWHPrice.toFixed(2))+parseFloat(sumServicePrice.toFixed(2))).toFixed(2)).appendTo($tr);
	$("#sbcChargingKWH").html(data.allTotalChargingKWH || 0);
	$("#sbcElectricCost").html(sumKWHPrice.toFixed(2));
	$("#sbcUServicePrice").html(sumServicePrice.toFixed(2));
	$("#sbcChargingCost").html((parseFloat(sumKWHPrice.toFixed(2))+parseFloat(sumServicePrice.toFixed(2))).toFixed(2));
	var effectiveDTTM = undefined;
	var chartIndex = 0;
	var forIndex = 0;
	for(var i = 0; i < subBranchCompanyBalanceData.length; i++){
		if(forIndex == 0 && (subBranchCompanyBalanceData[i].effectiveDTTM == effectiveDTTM || !effectiveDTTM)) {
			lineNameList.push(subBranchCompanyBalanceData[i].lineName);
			totalPriceData.push(parseFloat(subBranchCompanyBalanceData[i].totalPrice));
		} else if(subBranchCompanyBalanceData[i].effectiveDTTM != effectiveDTTM) {
			chartIndex = 0;
			totalPriceData[chartIndex] = floatObj.add(totalPriceData[chartIndex], parseFloat(subBranchCompanyBalanceData[i].totalPrice));
			forIndex++;
		} else {
			chartIndex++;
			totalPriceData[chartIndex] = floatObj.add(totalPriceData[chartIndex], parseFloat(subBranchCompanyBalanceData[i].totalPrice));
		}
		effectiveDTTM = subBranchCompanyBalanceData[i].effectiveDTTM;
	}
	changeEchart();
	setTimeout(hideLoading, 500);
}
function searchLineBalance(data){
	var carCountList = [];
	$("#lineChargingCount").html(data.totalChargingCount || 0);
	$("#lineChargingKWH").html(data.allTotalChargingKWH || 0);
	$("#lineChargingCost").html(data.totalChargingPrice || 0);
	lineBalanceData = data && data.LineBalanceList || [];
	var lineBalanceTbody = $("#lineBalanceTbody");
	lineBalanceTbody.empty();
	var length = lineBalanceData.length;
	var effectiveDTTM = undefined;
	var rowspanCount = undefined;
	var sumChargingKWH = 0;
	var sumBalanceKWH = 0;
	var sumKWHPrice = 0;
	var sumServicePrice = 0;
	for(var i=0; i<length + 1; i++){
		if (i == length || effectiveDTTM != lineBalanceData[i].effectiveDTTM) {
			effectiveDTTM = lineBalanceData[i] && lineBalanceData[i].effectiveDTTM;
			if (rowspanCount) {
				var rowCount = 1;
				for (var j=i - rowspanCount; j<i; j++) {
					sumChargingKWH = floatObj.add(sumChargingKWH, lineBalanceData[j].totalChargingKWH);
					sumBalanceKWH = floatObj.add(sumBalanceKWH, lineBalanceData[j].totalTradeKWH);
					sumKWHPrice = floatObj.add(sumKWHPrice, lineBalanceData[j].chargingPrice);
					sumServicePrice = floatObj.add(sumServicePrice, lineBalanceData[j].servicePrice);
					$tr = $('<tr>').appendTo(lineBalanceTbody);
					$("<td>").html(rowCount).appendTo($tr);
					rowCount++;
					$("<td>").html(lineBalanceData[j].chargingDate).appendTo($tr);
					$("<td>").html(lineBalanceData[j].chargingStationName).appendTo($tr);
					$("<td>").html(lineBalanceData[j].teamName).appendTo($tr);
					$("<td>").html(lineBalanceData[j].lineName).appendTo($tr);
					$("<td>").html(lineBalanceData[j].totalChargingKWH).appendTo($tr);
					$("<td>").html(lineBalanceData[j].balanceRatio).appendTo($tr);
					$("<td>").html(lineBalanceData[j].totalTradeKWH.toFixed(4)).appendTo($tr);
					$("<td>").html(lineBalanceData[j].chargingUnitPrice.toFixed(4)).appendTo($tr);
					$("<td>").html(lineBalanceData[j].chargingPrice.toFixed(4)).appendTo($tr);
					$("<td>").html(lineBalanceData[j].oilPrice).appendTo($tr);
					$("<td>").html(lineBalanceData[j].serviceUnitPrice.toFixed(4)).appendTo($tr);
					$("<td>").html(lineBalanceData[j].servicePrice.toFixed(4)).appendTo($tr);
					$("<td>").html(lineBalanceData[j].totalPrice.toFixed(4)).appendTo($tr);
				}
			}
			rowspanCount = 1;
		} else {
			rowspanCount++;
		}
	}
	$tr = $('<tr>').appendTo(lineBalanceTbody);
	$('<td colspan="5" style="font-weight: bold;">').html("总计").appendTo($tr);
	$("<td>").html(sumChargingKWH).appendTo($tr);
	$("<td>").html("——").appendTo($tr);
	$("<td>").html(sumBalanceKWH.toFixed(2)).appendTo($tr);
	$("<td>").html("——").appendTo($tr);
	$("<td>").html(sumKWHPrice.toFixed(2)).appendTo($tr);
	$("<td>").html("").appendTo($tr);
	$("<td>").html("——").appendTo($tr);
	$("<td>").html(sumServicePrice.toFixed(2)).appendTo($tr);
	$("<td>").html(data.totalChargingPrice).appendTo($tr);
	setTimeout(hideLoading, 500);
}
function searchStageSummary(data) {
	var stageSummaryData = data && data.stageSummaryList || [];
	createTableBody("stageSummaryTbody", stageSummaryTable, stageSummaryData, false, true);
}
function showMonth(index){
	$('.activeMonthLi').removeClass("activeMonthLi");
	$("#li"+ index).addClass("activeMonthLi");
	var year = $("#showYear").html().replace(/[^0-9]/ig,"");
	index = index >= 10 ? index : "0" + index;
	$("#balanceMonth").val(year + index + balanceDay);
	initChargingPriceName($("#ChargingPriceType").val());
//	$ajaxList('balanceBillName', getBalanceBillName, {BalanceMonth: $("#balanceMonth").val()});
}
function changeSumEchart(){
	$("#summaryHistogram").empty();
	if(subBranchCompanyNameData.length == 0){
		$("#summaryHistogram").attr("hidden",true);
		return;
	}
	$("#summaryHistogram").attr("hidden",false);
	myChart = echarts.init(document.getElementById('summaryHistogram'));
    var option = {
            tooltip: {
                trigger: 'axis'
            },
            xAxis: [
                {
                    type: 'category',
                    name: '分公司名称',
                    data: subBranchCompanyNameData
                }
            ],
            yAxis: [
                {
                    type: 'value',
                    name: '充电电量',
                    min: 0,
                    position: 'left',
                    axisLine: {
                        lineStyle: {
                            color: '#09be09',
                        }
                    },
                    axisLabel: {
                        formatter: '{value} '
                    }
                },
                {
                    type: 'value',
                    name: '充电次数',
                    min: 0,
                    position: 'right',
                    axisLine: {
                        lineStyle: {
                            color: '#01b2ac',
                        }
                    },
                    axisLabel: {
                        formatter: '{value}'
                    }
                },
                {
                    type: 'value',
                    name: '费用',
                    min: 0,
                    position: 'right',
                    offset: 80,
                    axisLine: {
                        lineStyle: {
                            color: '#fe902f',
                        }
                    },
                    axisLabel: {
                        formatter: '{value}'
                    }
                }
            ],
            series: [
                {
                    name:'充电电量',
                    type:'bar',
                    barWidth:10,
                    itemStyle: {
                        normal : {
                            color: '#09be09',
                        }
                    },
                    data:totalChargingKWHData
                },
                {
                	name:'充电次数',
                	type:'bar',
                	barWidth:10,
                	yAxisIndex: 1,
                	itemStyle: {
                        normal : {
                            color: '#01b2ac',
                        }
                    },
                	data: chargingCountData
                },
                {
                	name:'费用',
                	type:'bar',
                	barWidth:10,
                	yAxisIndex: 2,
                	itemStyle: {
                        normal : {
                            color: '#fe902f',
                        }
                    },
                	data: totalPriceData
                }
            ]
        };
    myChart.setOption(option);
}
function changeEchart(){
	$("#subBranchCompanyHistogram").empty();
	if(lineNameList.length == 0){
		$("#subBranchCompanyHistogram").attr("hidden",true);
		return;
	}
	$("#subBranchCompanyHistogram").attr("hidden",false);
	myChart = echarts.init(document.getElementById('subBranchCompanyHistogram'));
    var option = {
            tooltip: {
                trigger: 'axis'
            },
            xAxis: [
                {
                    type: 'category',
                    name: '线路名称',
                    data: lineNameList
                }
            ],
            yAxis: [
                {
                    type: 'value',
                    name: '费用',
                    min: 0,
                    axisLabel: {
                        formatter: '{value} '
                    }
                }
            ],
            series: [
                {
                    name:'费用',
                    type:'bar',
                    barWidth:10,
                    itemStyle: {
                        normal : {
                            color: '#09be09',
                        }
                    },
                    data:totalPriceData
                }
            ]
        };
    myChart.setOption(option);
}
function exportFile(flag){
	exportFlag = flag;
	if (flag == 1) {
		if (summaryBalanceData.length > 0) {
			showMessge("确定导出吗?", "model2");
		} else {
			showMessge("没有数据可导出！");
		}
	} else if (flag == 2) {
		if (subBranchCompanyBalanceData.length > 0) {
			showMessge("确定导出吗?", "model2");
		} else {
			showMessge("没有数据可导出！");
		}
	} else if (flag == 3) {
		if (lineBalanceData.length > 0) {
			showMessge("确定导出吗?", "model2");
		} else {
			showMessge("没有数据可导出！");
		}
	} else if(flag == "phaseSummary") {
		$ajax('exportPhaseSummary?checkFlag=', undefined, exportPhaseSummaryResult);
	} else if(flag == "stageSummary") {
		$ajax('exportStageSummary?checkFlag=', undefined, exportStageSummaryResult);
	}
}
function sure(){
	showMessge("导出中......");
	$ajax('exportSummaryBalance?checkFlag=&exportFlag=' + exportFlag + '&balanceDay=' + balanceDay, undefined, exportFileResult);
}
function exportFileResult(data){
	var result = data && data.result;
	if (result == "Success") {
		showLoading();
		downloadFile('exportSummaryBalance?checkFlag=OK&exportFlag=' + exportFlag);
		showMessge("导出成功，汇总结算文件正在生成！");
		setTimeout(hideLoading, 2000);
	} else if (result == "JsonError") {
		showMessge("导出失败，导出的JSON模板错误 或 不存在！");
	} else if (result == "NotFound") {
		showMessge("导出失败，没有数据可导出！");
	} else {
		showMessge("导出失败，请重新导出 或 联系管理员！");
	}
}
function exportPhaseSummaryResult(data) {
	var result = data && data.result;
	if (result == "Success") {
		showLoading();
		downloadFile('exportPhaseSummary?checkFlag=OK&');
		showMessge("导出成功，阶段汇总文件正在生成！");
		setTimeout(hideLoading, 2000);
	} else if (result == "JsonError") {
		showMessge("导出失败，导出的JSON模板错误 或 不存在！");
	} else if (result == "NotFound") {
		showMessge("导出失败，没有数据可导出！");
	} else {
		showMessge("导出失败，请重新导出 或 联系管理员！");
	}
}
function exportStageSummaryResult(data) {
	var result = data && data.result;
	if (result == "Success") {
		showLoading();
		downloadFile('exportStageSummary?checkFlag=OK&');
		showMessge("导出成功，建设期统计文件正在生成！");
		setTimeout(hideLoading, 2000);
	} else if (result == "JsonError") {
		showMessge("导出失败，导出的JSON模板错误 或 不存在！");
	} else if (result == "NotFound") {
		showMessge("导出失败，没有数据可导出！");
	} else {
		showMessge("导出失败，请重新导出 或 联系管理员！");
	}
}
function compareDate(DateOne, DateTwo, type) {
	var OneMonth = DateOne.substring(5, DateOne.lastIndexOf("-"));
	var OneDay = DateOne.substring(DateOne.length, DateOne.lastIndexOf("-") + 1);
	var OneYear = DateOne.substring(0, DateOne.indexOf("-"));
	var TwoMonth = DateTwo.substring(5, DateTwo.lastIndexOf("-"));
	var TwoDay = DateTwo.substring(DateTwo.length, DateTwo.lastIndexOf("-") + 1);
	var TwoYear = DateTwo.substring(0, DateTwo.indexOf("-"));
	if (Date.parse(OneMonth + "/" + OneDay + "/" + OneYear) > Date.parse(TwoMonth + "/" + TwoDay + "/" + TwoYear)) {
		if(type) {
			return DateOne;
		} else {
			return DateTwo;
		}
	} else {
		if(type){
			return DateTwo;
		} else {
			return DateOne;
		}
	}
}
function searchPhase() {
	var month = $("#balanceMonth").val().substring(4, 6) - 1;
	month = month >= 10 ? month : "0" + month;
	var startChargingTime = $("#balanceMonth").val().substring(0, 4) + "-" + month + "-" + balanceDay;
	var endChargingTime = $("#endChargingDate").val();
	if (!$("#subBranchCompanyPhase").val()) {
		$("#subBranchCompanyPhase").focus();
		$("#subBranchCompanyPhase").tips({
			side : 2,
			msg : "请选择分公司",
			bg : '#AE81FF',
			time : 1
		});
	} else if(!endChargingTime) {
		$("#endChargingDate").focus();
		$("#endChargingDate").tips({
			side : 2,
			msg : "请选择截止时间",
			bg : '#AE81FF',
			time : 1
		});
	} else if(stringToDate(startChargingTime) > stringToDate(endChargingTime)) {
		showMessge("截止时间小于开始时间！");
	} else {
		startChargingTime = date2String(stringToDate(startChargingTime));
		endChargingTime = date2String(stringToDate(endChargingTime));
		$ajax('searchPhaseSummary', {SubBranchCompanyID: $("#subBranchCompanyPhase").val(), StartChargingDate: startChargingTime, EndChargingDate: endChargingTime}, searchPhaseSummary);
	}
}
function searchPhaseSummary(data) {
	var phaseSummaryData = data && data.phaseSummaryList || [];
	var length = phaseSummaryData.length;
	clearTable("phaseSummaryTbody");
	var tbody = $("#phaseSummaryTbody");
	var chargingStationID = undefined;
	var rowspanCount = undefined;
	var number = 0;
	var totalSumKWH = 0;
	for(var i=0; i<length + 1; i++){
		var totalActiveKWH = 0;
		if (i == length || chargingStationID != phaseSummaryData[i].chargingStationID) {
			chargingStationID = phaseSummaryData[i] && phaseSummaryData[i].chargingStationID;
			if (rowspanCount) {
				for (var j=i - rowspanCount; j<i; j++) {
					number++;
					$tr = $('<tr>').appendTo(tbody);
					$('<td>').html(number).appendTo($tr);
					if (j==i - rowspanCount) {
						$('<td rowspan="'+ rowspanCount + '">').html(phaseSummaryData[i-1].chargingStationName).appendTo($tr);
					}
					$('<td>').html(phaseSummaryData[j].lineName).appendTo($tr);
					$('<td>').html(phaseSummaryData[j].teamName).appendTo($tr);
					$('<td>').html(phaseSummaryData[j].sumChargingKWH).appendTo($tr);
					totalActiveKWH = totalActiveKWH + phaseSummaryData[j].sumChargingKWH;
					totalSumKWH = totalSumKWH + phaseSummaryData[j].sumChargingKWH;
					if (j==i - rowspanCount) {
						$('<td rowspan="'+ rowspanCount + '" id="rowspan' + i + '">').appendTo($tr);
					}
					if (j == i - 1) {
						$("#rowspan" + i).html(totalActiveKWH);
					}
				}
			}
			rowspanCount = 1;
		} else {
			rowspanCount++;
		}
	}
	$tr = $('<tr>').appendTo(tbody);
	$('<td>').html(number + 1).appendTo($tr);
	$('<td>').html("总 计").appendTo($tr);
	$('<td>').html("-").appendTo($tr);
	$('<td>').html("-").appendTo($tr);
	$('<td>').html("-").appendTo($tr);
	$('<td>').html(totalSumKWH).appendTo($tr);
}
function exportAll(){
	if (!$("#subBranchCompanyName").val()) {
		$("#subBranchCompanyName").focus();
		$("#subBranchCompanyName").tips({
			side : 2,
			msg : "请选择分公司",
			bg : '#AE81FF',
			time : 1
		});
	} else {
		showMessge("导出中......");
		$ajax('exportAllSubBranchCompanyLine?checkFlag=', 'costBalanceSearch', exportAllResult);
	}
}
function exportAllResult(data){
	var result = data && data.result;
	if (result == "Success") {
		showLoading();
		downloadFile('exportAllSubBranchCompanyLine?checkFlag=OK');
		showMessge("导出成功，汇总结算文件正在生成！");
		setTimeout(hideLoading, 2000);
	} else if (result == "JsonError") {
		showMessge("导出失败，导出的JSON模板错误 或 不存在！");
	} else if (result == "NotFound") {
		showMessge("导出失败，没有数据可导出！");
	} else {
		showMessge("导出失败，请重新导出 或 联系管理员！");
	}
}